package cn.itcast.dao.impl;

import cn.itcast.dao.UserDao;
import cn.itcast.pojo.AdminUser;
import cn.itcast.pojo.User;
import cn.itcast.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author skycat
 * @time 2021/12/9
 */
public class UserDaoImpl implements UserDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<User> findAll() {
       String sql="select * from user";

        List<User> users = template.query(sql, new BeanPropertyRowMapper<User>(User.class));

        return users;

    }

    @Override
    public AdminUser findAdminUserByUserNameAndPassword(AdminUser user) {
        try {
            String sql="select * from adminuser where username=? and password=?";
            AdminUser adminUser = template.queryForObject(sql, new BeanPropertyRowMapper<AdminUser>
                    (AdminUser.class), user.getUsername(), user.getPassword());
            return adminUser;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void addUser(User user) {
        String sql="insert into user values(null,?,?,?,?,?,?)";
        template.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail());


    }

    @Override
    public void deleteUserById(int id) {
        String sql="delete from user where id=?";
        template.update(sql,id);

    }

    @Override
    public User findUserById(int id) {
        String sql="select * from user where id=?";
        User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

    @Override
    public void updateUser(User user) {
        String sql="update user set name=?,gender=?,age=?,address=?,qq=?,email=? where id=?";
        template.update(sql,user.getName(),user.getGender(),
                user.getAge(),user.getAddress(),user.getQq(),user.getEmail(),user.getId());
    }

    @Override
    public int findUserCount(Map<String, String[]> condition) {
        String sql="select count(*) from user where 1 = 1";
        StringBuilder sb=new StringBuilder(sql);
        //定义参数的集合
        List<Object> params = getSearchLikeObjects(condition, sb);

        Integer totalCount = template.queryForObject(sb.toString(),Integer.class, params.toArray());
        return totalCount;
    }

    private List<Object> getSearchLikeObjects(Map<String, String[]> condition, StringBuilder sb) {
        List<Object> params = new ArrayList<>();
        Set<String> keys = condition.keySet();
        for (String key : keys) {

            //排除分页条件参数
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }

            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                //value有值，搜索条件有值
                sb.append(" and " + key + " like ? ");
                params.add("%" + value + "%");//?的值还要加上模糊查询的%?%

            }
        }
        return params;
    }

    @Override
    public List<User> findUserListByPage(int start, int rows, Map<String, String[]> condition) {
        String sql="select * from user where 1 = 1 ";
        StringBuilder sb=new StringBuilder(sql);
        //定义参数的集合
        List<Object> params = getSearchLikeObjects(condition, sb);
        //添加分页查询
        sb.append(" limit ?,?");
        params.add(start);
        params.add(rows);

        List<User> list = template.query(sb.toString(), new BeanPropertyRowMapper<User>(User.class),params.toArray() );
        return list;
    }


}
